//
//  QinMessageInputView.m
//  自定义聊天界面和键盘-多选照片-语音-表情-文本
//
//  Created by qinlongqing on 14-10-13.
//  Copyright (c) 2014年 qinlongqing. All rights reserved.
//

#import "QinMessageInputView.h"
#import "QinKeyBoardController.h"

@interface QinMessageInputView ()
{

}
@end

@implementation QinMessageInputView

- (instancetype)init{
    self = [super init];
    if (self) {
        [self setupSubView];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupSubView];
    }
    return self;
}

- (void)setupSubView{
#pragma 发送音频的按钮
    _sendAudioButton = [[UIButton alloc]initWithFrame:CGRectMake(10,CGRectGetHeight(self.frame)- 5-35, 35, 35)];
    [_sendAudioButton setImage:[UIImage imageNamed:@"keyboard_Input_voice"] forState:UIControlStateNormal];
    [_sendAudioButton setImage:[UIImage imageNamed:@"keyboard_send_text"] forState:UIControlStateSelected];
    [_sendAudioButton addTarget:self action:@selector(doVoiceButton:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_sendAudioButton];
//    [_sendAudioButton release];
    
#pragma 输入文本的textField
    self.inputTextView = [[UITextView alloc]initWithFrame:CGRectMake(50, (CGRectGetHeight(self.frame) - 35)/2, kScreenWidth-100, 35)];
    [self.inputTextView.layer setCornerRadius:4];
    [self.inputTextView setFont:[UIFont systemFontOfSize:15]];
    self.inputTextView.layer.borderColor = [ClassMethodsViewController colorWithHexString:LINE_BACKGROUNDCOLOR_16].CGColor;
    
    self.inputTextView.returnKeyType = UIReturnKeyDone;
    self.inputTextView.layer.borderWidth =1.0;
    self.inputTextView.layer.cornerRadius =5.0;
    [self addSubview:self.inputTextView];
    
#pragma 当选中发送音频时，出现的显示或者隐藏自定义键盘
    _showOrHiddenSendAudioButton = [[UIButton alloc]initWithFrame:CGRectMake(50,(CGRectGetHeight(self.frame) - 35)/2, kScreenWidth-100, 35)];
    [_showOrHiddenSendAudioButton setImage:[UIImage imageNamed:@"聊天_箭头"] forState:UIControlStateNormal];
    [_showOrHiddenSendAudioButton setImageEdgeInsets:UIEdgeInsetsMake(0,0, 0, 0)];
    [_showOrHiddenSendAudioButton setImage:[UIImage imageNamed:@"聊天_向上箭头"] forState:UIControlStateSelected];
    [_showOrHiddenSendAudioButton addTarget:self action:@selector(doShowOrHiddenSendAudioButton:) forControlEvents:UIControlEventTouchUpInside];
    _showOrHiddenSendAudioButton.hidden = YES;
    [self addSubview:_showOrHiddenSendAudioButton];
//    [_showOrHiddenSendAudioButton release];

#pragma 发送表情图片的按钮
    _sendFaceButton = [[UIButton alloc]initWithFrame:CGRectMake(kScreenWidth-90, CGRectGetHeight(self.frame)-5-35, 35, 35)];
    [_sendFaceButton setImage:[UIImage imageNamed:@"keyboard_take_face"] forState:UIControlStateNormal];
    [_sendFaceButton setImage:[UIImage imageNamed:@"keyboard_send_text"] forState:UIControlStateSelected];
    [_sendFaceButton addTarget:self action:@selector(doSendFaceButton:) forControlEvents:UIControlEventTouchUpInside];
//    [self addSubview:_sendFaceButton];
//    [_sendFaceButton release];
    
#pragma 发送图片的按钮
    _sendPhotoButton = [[UIButton alloc]initWithFrame:CGRectMake(kScreenWidth-45,CGRectGetHeight(self.frame)- 5-35, 35, 35)];
    [_sendPhotoButton setImage:[UIImage imageNamed:@"keyboard_to_take_pictureOrPhotos"] forState:UIControlStateNormal];
    [_sendPhotoButton setImage:[UIImage imageNamed:@"keyboard_send_text"] forState:UIControlStateSelected];
    [_sendPhotoButton addTarget:self action:@selector(doSendPhotoButton:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_sendPhotoButton];
//    [_sendPhotoButton release];
}

- (void)doShowOrHiddenSendAudioButton:(UIButton *)senderButton{
    if (senderButton.selected) {//点击出现发送语音的界面
        [[QinKeyBoardController instance] showCustomKeyboardToolAnimation:sendAudioViewType];
    }
    else{//点击隐藏发送语音的界面
        [[QinKeyBoardController instance] hiddenCustomKeyboardToolAnimation];
    }
    senderButton.selected = !senderButton.selected;
}

- (void)doVoiceButton:(UIButton *)senderButton{
//    if (self.changevoiceDelegate&&[self.changevoiceDelegate respondsToSelector:@selector(changeBTstatus:)]) {
//        [self.changevoiceDelegate changeBTstatus:senderButton];
//    }
    if (senderButton.selected) {//点击显示发送文本
        _showOrHiddenSendAudioButton.hidden = YES;
        self.inputTextView.hidden = NO;
        [[QinKeyBoardController instance] showSystemKeyboardToolAnimation:NO withSender:senderButton];//显示系统键盘
    }
    else{//点击显示发送音频
        _showOrHiddenSendAudioButton.hidden = NO;
        _showOrHiddenSendAudioButton.selected = NO;
        self.inputTextView.hidden = YES;
        _sendFaceButton.selected = NO;
        _sendPhotoButton.selected = NO;
        
        [[QinKeyBoardController instance] showCustomKeyboardToolAnimation:sendAudioViewType];//显示自定义键盘
    }
    senderButton.selected = !senderButton.selected;
}

- (void)doSendFaceButton:(UIButton *)senderButton{
    if (senderButton.selected) {//点击显示发送文本
        _showOrHiddenSendAudioButton.hidden = YES;
        self.inputTextView.hidden = NO;
        [[QinKeyBoardController instance] showSystemKeyboardToolAnimation:NO withSender:senderButton];//显示系统键盘
    }
    else{//点击显示发送表情
        _showOrHiddenSendAudioButton.hidden = YES;
        self.inputTextView.hidden = NO;
        _sendAudioButton.selected = NO;
        _sendPhotoButton.selected = NO;
        [[QinKeyBoardController instance] showCustomKeyboardToolAnimation:sendFaceViewType];//显示自定义键盘
    }
    senderButton.selected = !senderButton.selected;
}

- (void)doSendPhotoButton:(UIButton *)senderButton{
    if (senderButton.selected) {//点击显示发送文本
        _showOrHiddenSendAudioButton.hidden = YES;
        self.inputTextView.hidden = NO;
        [[QinKeyBoardController instance] showSystemKeyboardToolAnimation:NO withSender:senderButton];//显示系统键盘
    }
    else{//点击显示发送图片界面
        _showOrHiddenSendAudioButton.hidden = YES;
        self.inputTextView.hidden = NO;
        _sendAudioButton.selected = NO;
        _sendFaceButton.selected = NO;
        [[QinKeyBoardController instance] showCustomKeyboardToolAnimation:sendPhotoViewType];//显示自定义键盘
    }
    senderButton.selected = !senderButton.selected;
}
/******************************分割线******************************/
- (NSString *)trimWhitespace
{
    return [self.inputTextView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

- (NSUInteger)numberOfLines
{
    return [self.inputTextView.text componentsSeparatedByString:@"\n"].count + 1;
}
//________________________
#pragma mark - Message text view

- (NSUInteger)numberOfLinesOfText
{
    return [self numberOfLinesForMessage:self.inputTextView.text];
}

- (NSUInteger)maxCharactersPerLine
{
    return ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone) ? 28 : 109;
}

- (NSUInteger)numberOfLinesForMessage:(NSString *)text
{
    return (text.length / [self maxCharactersPerLine]) + 1;
}
- (void)resetSubviewsFrame
{
    self.sendAudioButton.frame=CGRectMake(10,CGRectGetHeight(self.frame)- 5-35, 35, 35);
    self.sendFaceButton.frame=CGRectMake(kScreenWidth-90,CGRectGetHeight(self.frame)- 5-35, 35, 35);
    self.sendPhotoButton.frame=CGRectMake(kScreenWidth-45,CGRectGetHeight(self.frame)- 5-35, 35, 35);
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
